﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using FileSystemControls.ListViewItems;

namespace FileSystemControls
{
    // Implements the manual sorting of items by column.
    class FileSystemListViewItemComparer : IComparer
    {
        private int col;
        private SortOrder order;

        public FileSystemListViewItemComparer()
        {
            col = 0;
            order = SortOrder.Ascending;
        }

        public FileSystemListViewItemComparer(int column, SortOrder order)
        {
            col = column;
            this.order = order;
        }

        public int Compare(object x, object y)
        {
            if ((x is FileSystemListViewItem) && (y is FileSystemListViewItem))
            {
                return Compare((FileSystemListViewItem)x, (FileSystemListViewItem)y);
            }
            else
            {
                return 0;
            }
        }

        public int Compare(FileSystemListViewItem x, FileSystemListViewItem y)
        {
            // -1 = First > Second
            //  0 = First = Second
            //  1 = First < Second

            int returnVal = -1;
            FileSystemListViewItem FirstListViewItem;
            FileSystemListViewItem SecondListViewItem;

            FirstListViewItem = (FileSystemListViewItem)x;
            SecondListViewItem = (FileSystemListViewItem)y;

            //if (FirstListViewItem.IsDotDot)
            if (FirstListViewItem is DotDotListViewItem)
            {
                return -1;
            }
            //if (SecondListViewItem.IsDotDot)
            if (SecondListViewItem is DotDotListViewItem)
            {
                return 1;
            }

            //If one item is a directory and the other isn't - Directory comes first.
            if (FirstListViewItem is DirectoryListViewItem) //Not a file == Directory
            {
                if (SecondListViewItem is FileListViewItem) //Second is a file
                {
                    return -1;
                }
            }
            else //Else, if the first is a file
            {
                if (SecondListViewItem is DirectoryListViewItem) //...and the second is not a file
                {
                    return 1;
                }
            }

            switch (col)
            {
                case 0: //Name
                    returnVal = String.Compare(FirstListViewItem.Name, SecondListViewItem.Name);
                    break;
                case 1: //Size
                    //Since I got rid of File/Dir conflicts up above it's a matter of  
                    //what to do with dirs since we don't have a size for them.
                    //Since I eliminated File/Dir conflicts above if one item is a dir the other is too.
                    if (FirstListViewItem.Length == SecondListViewItem.Length)
                    {
                        returnVal = 0;
                    }
                    else if (FirstListViewItem.Length > SecondListViewItem.Length)
                    {
                        returnVal = -1;
                    }
                    else
                    {
                        returnVal = 1;
                    }
                    break;
                case 2: //Date

                    returnVal = DateTime.Compare(FirstListViewItem.ModifyDate, SecondListViewItem.ModifyDate);
                    break;
            }

            // Determine whether the sort order is descending.
            if (order == SortOrder.Descending)
                // Invert the value returned by String.Compare.
                returnVal *= -1;
            return returnVal;
        }

    }

    public class FileSystemPathChangingEventArgs : EventArgs
    {
        //Nothing I can think of right now.
    }

    public class FileSystemPathChangedEventArgs : EventArgs
    {
        string _path;

        public string NewPath
        {
            get
            {
                return _path;
            }
        }

        public FileSystemPathChangedEventArgs(string NewPath)
        {
            _path = NewPath;
        }
    }

    public class AddItemProgressEventArgs : EventArgs
    {
        public int CurrentPercentage
        {
            get;
            private set;
        }

        public FileSystemListViewItem ListItem
        {
            get;
            private set;
        }

        public AddItemProgressEventArgs(int Percentage, FileSystemListViewItem NewItem)
        {
            CurrentPercentage = Percentage;
            ListItem = NewItem;
        }

    }

    public class AddItemCompleteEventArgs:EventArgs
    {
        public long AvailableFreeSpace { get; private set; }
        public AddItemCompleteEventArgs(long argAvailableFreeSpace)
        {
            AvailableFreeSpace = argAvailableFreeSpace;
        }
    }

    public class CopyProgressEventArgs : EventArgs
    {
        public long CopyPosition
        {
            get;
            set;
        }

        public CopyProgressEventArgs(long Position)
        {
            CopyPosition = Position;
        }
    }

    //public class FileSystemListViewItemsCollection : List<FileSystemListViewItem>
    //{
    //    //List<FileSystemListViewItem> _internalList;

    //    public FileSystemListViewItemsCollection()
    //    {
    //        //_internalList = new List<FileSystemListViewItem>();
    //    }

    //    public FileSystemListViewItemsCollection(IEnumerable<FileSystemListViewItem> NewList)
    //    {
    //        //_internalList = NewList;
    //        AddRange(NewList);
    //    }
        
    //    //public void Add(FileSystemListViewItem NewItem)
    //    //{
    //    //    //_internalList.Add(NewItem);
    //    //}

    //    //public System.Collections.Generic.IEnumerator<FileSystemListViewItem> GetEnumerator()
    //    //{
    //    //    foreach (FileSystemListViewItem CurrentItem in _internalList)
    //    //    {
    //    //        yield return CurrentItem;
    //    //    }
    //    //}

    //    //public void Clear()
    //    //{
    //    //    _internalList.Clear();
    //    //}
    //}

    //public class FileSystemListViewSelectedItemsCollection : FileSystemListViewItemsCollection //List<FileSystemListViewItem>
    //{
    //    public FileSystemListViewSelectedItemsCollection()
    //        : base()
    //    {
    //    }

    //    public new void Add(FileSystemListViewItem CurrentItem)
    //    {
    //        CurrentItem.IsMarked = true;
    //        base.Add(CurrentItem);
    //    }

    //    public new bool Remove(FileSystemListViewItem CurrentItem)
    //    {
    //        CurrentItem.IsMarked = false;
    //        return base.Remove(CurrentItem);
    //    }

    //    public new void Clear()
    //    {
    //        foreach (FileSystemListViewItem CurrentItem in this)
    //        {
    //            CurrentItem.IsMarked = false;
    //        }
    //        base.Clear();
    //    }

    //}

    //public class FileListViewItemsCollection : FileSystemListViewItemsCollection
    //{
    //    public FileListViewItemsCollection()
    //        : base()
    //    {
    //    }

    //    public static implicit operator FileSystemListViewItemsCollection(FileListViewItemsCollection CurrentCollection)
    //    {
    //        FileSystemListViewItemsCollection NewCollection;
    //        NewCollection = new FileSystemListViewSelectedItemsCollection();

    //        foreach (FileListViewItem CurrentItem in CurrentCollection)
    //        {
    //            NewCollection.Add(CurrentItem);
    //        }

    //        return NewCollection;
    //    }

    //    public new void Add(FileListViewItem CurrentItem)
    //    {
    //        CurrentItem.IsMarked = true;
    //        base.Add(CurrentItem);
    //    }

    //    public new bool Remove(FileListViewItem CurrentItem)
    //    {
    //        CurrentItem.IsMarked = false;
    //        return base.Remove(CurrentItem);
    //    }

    //    public new void Clear()
    //    {
    //        foreach (FileListViewItem CurrentItem in this)
    //        {
    //            CurrentItem.IsMarked = false;
    //        }
    //        base.Clear();
    //    }
    //}

    //public class DirectoryListViewSelectedItemsCollection : List<DirectoryListViewItem>
    //{
    //    public DirectoryListViewSelectedItemsCollection()
    //        : base()
    //    {
    //    }

    //    //same as (FileSystemListViewSelectedItemsCollection)DirectoryListViewSelectedItemsCollection
    //    public static implicit operator FileSystemListViewSelectedItemsCollection(DirectoryListViewSelectedItemsCollection CurrentCollection)
    //    {
    //        FileSystemListViewSelectedItemsCollection NewCollection = new FileSystemListViewSelectedItemsCollection();

    //        foreach (DirectoryListViewItem CurrentItem in CurrentCollection)
    //        {
    //            NewCollection.Add(CurrentItem);
    //        }

    //        return NewCollection;
    //    }

    //    public new void Add(DirectoryListViewItem CurrentItem)
    //    {
    //        //Not needed. DotDotListViewItems are now different objects
    //        //if (CurrentItem.IsDotDot)
    //        //    return;

    //        CurrentItem.IsMarked = true;
    //        base.Add(CurrentItem);
    //    }

    //    public new bool Remove(DirectoryListViewItem CurrentItem)
    //    {
    //        CurrentItem.IsMarked = false;
    //        return base.Remove(CurrentItem);
    //    }

    //    public new void Clear()
    //    {
    //        foreach (DirectoryListViewItem CurrentItem in this)
    //        {
    //            CurrentItem.IsMarked = false;
    //        }
    //        base.Clear();
    //    }
    //}

}